package org.reptile.service;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.reptile.model.User;
import org.reptile.util.ReptileUtil;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author : lsliang
 * @Description : 解析排名信息
 * @Date :  Create in 20:49 2017/12/15
 * @Modifyed By :
 */
@Service
public class AnalyticService {

    /**
     * 获取所有的排名信息
     * @return
     */
    public List<User> getUsers(){
        return getUsers(1000);
    }

    /**
     * 获取排名信息
     * @return
     */
    public List<User> getUsers(int count){
        Document document = ReptileUtil.getDocument("http://zhuanti.7gz.com/?mod=activity_api&act=votelist&activity_id=1&topic_id=665");
        Elements uls = document.getElementsByClass("vote_list_ls");
        List<User> users = new ArrayList<User>();
        int i = 1;
        AA : for (Element ul : uls){
            Elements lis = ul.getElementsByTag("li");
            for (Element li : lis){
                StringBuilder result = new StringBuilder();
                Elements divs = li.getElementsByTag("div");
                for (Element div : divs){
                    if ("".equals(div.getElementsByTag("strong").text())){
                        result.append(div.text() + ",");
                    }else{
                        result.append(div.getElementsByTag("strong").text() + ",");
                        result.append(div.getElementsByTag("span").text() + ",");
                    }
                }
                String[] us = result.substring(0, result.length() - 1).split(",");
                User user = new User();
                user.setBallot(Integer.valueOf(us[0]));
                user.setName(us[1]);
                user.setProvince(us[2]);
                user.setRanking(Integer.valueOf(us[3]));
                user.setProportion(us[4]);
                users.add(user);
                i++;
                if (i > count){
                    break AA;
                }
            }
        }
        return users;
    }
}
